package com.hxxy.wyp.petsLover.common.rest.base.backend;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.Orders;
import com.hxxy.wyp.petsLover.common.entities.User;
import com.hxxy.wyp.petsLover.common.entities.vo.req.SearchObj;
import com.hxxy.wyp.petsLover.common.entities.vo.req.SearchObjOrder;
import com.hxxy.wyp.petsLover.common.services.base.backend.OrderService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName OrderRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/1/29 15:15
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/order")
public class OrderRest extends RestBase {
    @Autowired
    private OrderService orderService;

    @SaCheckLogin
    @SaCheckRole(value = {"commodity_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"commodity_load", "all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Object page(@RequestBody SearchObjOrder searchObj) {
        long page = Long.parseLong(searchObj.getPage());
        long pageSize = Long.parseLong(searchObj.getPageSize());
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObj.getOrderId()), Orders::getOrderId, searchObj.getOrderId());
        lambdaQueryWrapper.like(Strings.isNotBlank(searchObj.getOrderPhone()), Orders::getOrderPhone, searchObj.getOrderPhone());
        lambdaQueryWrapper.ge(searchObj.getCreateTimeBegin() != null, Orders::getOrderPurchaseTime, searchObj.getCreateTimeBegin());
        lambdaQueryWrapper.le(searchObj.getCreateTimeEnd() != null, Orders::getOrderPurchaseTime, searchObj.getCreateTimeEnd());
        lambdaQueryWrapper.eq(searchObj.getOrderStatus() != null, Orders::getOrderStatus, searchObj.getOrderStatus());
        lambdaQueryWrapper.orderByDesc(Orders::getOrderPaymentTime);
        orderService.pageSearch(pageInfo, lambdaQueryWrapper);
        return ajaxSuccess(pageInfo, "200", "查询成功！");
    }

    @SaCheckLogin
    @SaCheckRole(value = {"commodity_admin", "super_admin"}, mode = SaMode.OR)
    @SaCheckPermission(value = {"commodity_load", "all"}, mode = cn.dev33.satoken.annotation.SaMode.OR)
    @RequestMapping(value = "/updateIsSend")
    public Object updateISend(@RequestParam("id") String id) {
        Orders orders = orderService.getById(id);
        if ("0".equals(orders.getIsSend())) {
            orders.setIsSend("1");
        }
        boolean b = orderService.updateById(orders);
        if (b) {
            return ajaxSuccess("200", "发货成功!");
        } else {
            return ajaxError("500", "发货失败");
        }

    }
}
